package com.gitee.kenewstar.utils;

/**
 * @author kenewstar
 * @date 2021/7/9
 */
public abstract class CommonUtil {

	public interface Constant {
		long KB = 1024L;
		long MB = KB * KB;
		long GB = MB * KB;
		long TB = GB * KB;
		String FORMAT = "%.2f";
	}

	/**
	 * <p>
	 *     将字节数转换为文件大小的字符串
	 *     1024  -> 1KB
	 *     10240 -> 10KB
	 * </p>
	 * @param bytes 字节数
	 * @return 文件大小字符串
	 */
	public static String byte2Size(long bytes) {
		String size;
		if (bytes < Constant.KB) {
			size = bytes + "B";
		} else if (bytes < Constant.MB) {
			size = String.format(Constant.FORMAT, (double) bytes / Constant.KB) + "KB";
		} else if (bytes < Constant.GB) {
			size = String.format(Constant.FORMAT, (double) bytes / Constant.MB) + "MB";
		} else if (bytes < Constant.TB) {
			size = String.format(Constant.FORMAT, (double) bytes / Constant.GB) + "GB";
		} else {
			size = String.format(Constant.FORMAT, (double) bytes / Constant.TB) + "TB";
		}
		return size;
	}
}
